package com.dykj.fireFighting.web.mongodb;


import com.dykj.fireFighting.api.base.file.entity.DataFiles;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.jboss.marshalling.ByteInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * @Author:zhujunming
 * @Date: 2018/10/24 17:29
 * @For: Mongo 大文件存储
 **/
@Repository
public class GridFSRepository {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    GridFsTemplate gridFsTemplate;
    @Autowired
    public GridFSBucket gridFSBucket;

    /**
     * 保存文件InputStream
     *
     *
     */
    public DataFiles saveFile(MultipartFile file) {

        DataFiles dataFiles=new DataFiles();

        try {
            DBObject metaData = new BasicDBObject();
            ((BasicDBObject) metaData).put("file_id",UUID.randomUUID());
            ((BasicDBObject) metaData).put("createTime",new Date());
            Object id =gridFsTemplate.store(file.getInputStream(),file.getOriginalFilename(),file.getContentType(),metaData);
            dataFiles.setId(UUIDUtils.get32UUID());
            dataFiles.setCreateTime(new Date());
            dataFiles.setFileMongoId(id.toString());
            dataFiles.setFileName(file.getOriginalFilename());
            dataFiles.setFileSize(String.valueOf(file.getSize()));
            dataFiles.setFileType(file.getContentType());


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    return  dataFiles;


    }
    public DataFiles saveFile(ByteArrayInputStream ins, String fileName, int length,String contentType)
    {
        DataFiles dataFiles=new DataFiles();

        try {
            DBObject metaData = new BasicDBObject();
            ((BasicDBObject) metaData).put("file_id",UUID.randomUUID());
            ((BasicDBObject) metaData).put("createTime",new Date());
            Object id =gridFsTemplate.store(ins,fileName,contentType,metaData);
            dataFiles.setId(UUIDUtils.get32UUID());
            dataFiles.setCreateTime(new Date());
            dataFiles.setFileMongoId(id.toString());
            dataFiles.setFileName(fileName);
            dataFiles.setFileSize(String.valueOf(length));
            dataFiles.setFileType(contentType);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return  dataFiles;
    }


    /**
     * 查询文件（主键）
     *
     * @date 2015年12月1日下午5:30:45
     */
    public GridFSFile findOne(String id) {
        GridFSFile gridFSFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
        return gridFSFile;

    }


    /**
     * 删除文件
     * @param id
     * @return
     */
    public boolean delete(String id)
    {
       try {
           gridFsTemplate.delete(new Query().addCriteria(Criteria.where("_id").is(id)));
           return  true;
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }


    }

}
